#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<wait.h>
#include<string.h>
#include<sys/types.h>
/*
int main()
{
    printf("before : I am a process, pid : %d,ppid : %d\n",getpid(),getppid());

    //在/usr/bin/ls路径下执行ls -a -l
    execl("/usr/bin/ls", "ls" , "-a" , "-l", NULL);

    printf("after : I am a process, pid : %d,ppid : %d\n",getpid(),getppid());
    
    return 0;
}
*/

/*
int main()
{
    pid_t id=fork();
    extern char**environ;
    char*argv[] = {"ls","-a","-l",NULL};
    if(id==0)
    {
        //子进程 
        printf("I am a child, pid : %d,ppid : %d\n",getpid(),getppid());
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        
        //execle("/usr/bin/ls","ls","-a","-l",environ);
       
        char*const argv[] = {"ls","-a","-l",NULL};
        execv("/usr/bin/ls",argv);
        sleep(1);
        exit(11);  //如果替换失败，就会执行下面的代码
    }
    //父进程
    else if(id>0)
    {
        printf("I am a parent, pid : %d,ppid : %d\n",getpid(),getppid());
        int status =0;
        pid_t ret=waitpid(-1,&status,0);

        if(ret==id)
        { 
            printf("等待成功, pid : %d，exit_code: %d ,exit_sig: %d\n",getpid(),(status>>8)&0xFF,status&0x7F);
            sleep(1);
        }
    }
    return 0;
}

*/

int main()
{
    pid_t id=fork();
    extern char**environ;
     char*argv[] = {"ls","-a","-l",NULL};
    if(id==0)
    {
        //子进程 
        printf("I am a child, pid : %d,ppid : %d\n",getpid(),getppid());
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        
        //execle("/usr/bin/ls","ls","-a","-l",environ);

        execv("/usr/bin/ls",argv);
        sleep(1);
        exit(11);  //如果替换失败，就会执行下面的代码
    }
    //父进程
    else if(id>0)
    {
        printf("I am a parent, pid : %d,ppid : %d\n",getpid(),getppid());
        int status =0;
        pid_t ret=waitpid(-1,&status,0);

        if(ret==id)
        { 
            printf("等待成功, pid : %d，exit_code: %d ,exit_sig: %d\n",getpid(),(status>>8)&0xFF,status&0x7F);
            sleep(1);
        }
    }
}
